package com.woniuxy.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.base.mapper.BaseCategoryMapper;
import com.woniuxy.base.model.entity.BaseCategory;
import com.woniuxy.base.model.entity.BaseCategoryRecord;
import com.woniuxy.base.service.BaseCategoryRecordService;
import com.woniuxy.base.service.BaseCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BaseCategoryServiceImpl implements BaseCategoryService {
    @Resource
    private BaseCategoryMapper baseCategoryMapper;

    @Resource
    private BaseCategoryRecordService recordService;

    @Override
    public Page<BaseCategory> getAll(int current, int size) {
        return baseCategoryMapper.selectPage(new Page<>(current, size),null);
    }

    @Transactional
    @Override
    public Boolean update(BaseCategory baseCategory, String account) {
        BaseCategory record = baseCategoryMapper.selectById(baseCategory.getId());
        BaseCategoryRecord categoryRecord = new BaseCategoryRecord();
        categoryRecord.setType(record.getType())
                        .setState(record.getState())
                        .setDescription(record.getDescription()).setUpdateTime(record.getAddTime())
                        .setCategoryId(record.getId()).setUpdatePerson(account);
        baseCategoryMapper.update(baseCategory, new UpdateWrapper<BaseCategory>().eq("id",baseCategory.getId()));
        recordService.add(categoryRecord);
        return true;
    }

    @Override
    public Boolean add(BaseCategory baseCategory) {
        baseCategoryMapper.insert(baseCategory);
        return true;
    }

    @Override
    public Page<BaseCategory> conditionSearch(BaseCategory baseCategory, Integer current, Integer size) {
        return baseCategoryMapper.selectPage(new Page<>(current,size),new QueryWrapper<BaseCategory>(baseCategory));
    }

    @Override
    public Page<BaseCategory> keywordSearch(String keyword, Integer current, Integer size) {
        QueryWrapper<BaseCategory> wrapper = new QueryWrapper<>();
        wrapper.like("type",keyword).or().like("description",keyword);
        return baseCategoryMapper.selectPage(new Page<>(current,size),wrapper);
    }

    @Override
    public List<BaseCategoryRecord> detail(Integer categoryId) {
        return recordService.detail(categoryId);
    }

}
